package com.wx.mall.goods.controller;


import com.wx.mall.common.api.CommonPage;
import com.wx.mall.common.api.CommonResult;
import com.wx.mall.common.api.ResultCode;
import com.wx.mall.common.exceptions.ApiAssert;
import com.wx.mall.common.utils.DataResult;
import com.wx.mall.goods.entity.Category;
import com.wx.mall.goods.entity.CategoryVo;
import com.wx.mall.goods.service.ICategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 商品分类表
 * 前端控制器
 * </p>
 *
 * @author j
 * @since 2020-03-15
 */
@Api(tags = "商品分类", description = "CategoryController")
@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {

    @Resource
    private ICategoryService service;


    @ApiOperation("获取分页的全部的分类")
    @GetMapping("/listAll")
    public CommonResult<CommonPage<Category>> getListAll(@RequestParam(value = "pageNum") Integer pageNum,
                                                         @RequestParam(value = "pageSize") Integer pageSize) {
        if (pageNum == null || pageSize == null) {
            ApiAssert.fail(ResultCode.VALIDATE_FAILED); // 直接抛异常，使用enum
        }
        if (pageNum <= 0) {
            ApiAssert.fail("无效页码"); // 直接抛异常，使用自定义的message
        }
        List<Category> list = service.getListAll(pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(list)); // 返回分页查询结果
    }


    @ApiOperation("获取递归分类")
    @GetMapping("/listAllVo")
    public DataResult getListAllVo() {
        List<CategoryVo> list = service.tree();
        return DataResult.success(list);
    }

    @ApiOperation("分页根据父级id进行查询下层子集")
    @GetMapping("/childrenListAll")
    public CommonResult<CommonPage<Category>> getChildrenAll(Integer pageNum,
                                                              Integer pageSize,
                                                              Integer categoryParentId) {
        System.out.println(pageNum);
        if (pageNum == null || pageSize == null) {
            ApiAssert.fail(ResultCode.VALIDATE_FAILED); // 直接抛异常，使用enum
        }
        if (pageNum <= 0) {
            ApiAssert.fail("无效页码"); // 直接抛异常，使用自定义的message
        }
        List<Category> list = service.getChildrenAll(pageNum, pageSize, categoryParentId);
        return CommonResult.success(CommonPage.restPage(list)); // 返回分页
    }



    @ApiOperation("根据id进行查询")
    @GetMapping("/listAllById")
    public DataResult getListAllById(Integer categoryId) {
        Category list = service.getListAllById(categoryId);
        return DataResult.success(list);
    }

    @ApiOperation("根据id进行查询")
    @GetMapping("/list/parentId")
    public DataResult getListByParent(Integer parentId) {
        List<Category> list = service.getListByParent(parentId);
        return DataResult.success(list);
    }

    @ApiOperation("修改任何字段数据包括（父级id）")
    @PutMapping("/updateParentId")
    public DataResult updateParentId(Category category) {
        //0为成功1为失败
        int success = service.updateParentId(category);
        return success == 0 ? DataResult.success() : DataResult.getResult(1, "修改失败");
    }

    @ApiOperation("根据id查询分类名")
    @GetMapping("/getCategoryName")
    public DataResult getCategoryName(Integer categoryId) {

        String categoryName = service.getCategoryName(categoryId);
        return DataResult.success(categoryName);
    }

    @ApiOperation("查询所有分类名")
    @GetMapping("/getCategoryNameAll")
    public DataResult getCategoryNameAll() {

        List<String> categoryName = service.getCategoryNameAll();
        return DataResult.success(categoryName);
    }
    @ApiOperation("查询所有的东西")
    @GetMapping("/getCategoryAll")
    public DataResult getCategoryAll() {

        List<Category> categoryName = service.getCategoryAll();
        return DataResult.success(categoryName);
    }

    @ApiOperation("根据分类名查询")
    @GetMapping("/getCategoryValue")
    public DataResult getCategoryValue(String categoryName) {

        Category list = service.getCategoryValue(categoryName);
        return DataResult.success(list);
    }

    @ApiOperation("根据id查询级别")
    @GetMapping("/getCategoryLevel")
    public DataResult getCategoryLevel(Integer categoryId) {
        log.info("ss");
        int categoryLevel = service.getCategoryLevel(categoryId);
        return DataResult.success(categoryLevel);
    }

    @ApiOperation("插入一条信息")
    @PostMapping("/insertForm")
    public DataResult setForm(Category category) {
        System.out.println(category);
        //0为成功1为失败ss
        int success = service.setForm(category);
        return success == 0 ? DataResult.success() : DataResult.getResult(1, "插入失败");
    }

    @ApiOperation("根据级别查询")
    @GetMapping("/listAllByLevel")
    public DataResult getListByLevel(Integer categoryLevel) {
        List<Category> list = service.getListByLevel(categoryLevel);
        return DataResult.success(list);
    }

    @ApiOperation("根据级别查询并分页")
    @GetMapping("/listAllByLevelPage")
    public CommonResult<CommonPage<Category>> getListByLevelPage(Integer pageNum,
                                                                 Integer pageSize,Integer categoryLevel) {

            System.out.println(pageNum);
            if (pageNum == null || pageSize == null) {
                ApiAssert.fail(ResultCode.VALIDATE_FAILED); // 直接抛异常，使用enum
            }
            if (pageNum <= 0) {
                ApiAssert.fail("无效页码"); // 直接抛异常，使用自定义的message
            }
            List<Category> list = service.getListByLevelPage(pageNum, pageSize,categoryLevel);
            return CommonResult.success(CommonPage.restPage(list)); // 返回分页
    }

    @ApiOperation("查询出来根据排序来升降")
    @GetMapping("/listAllBySort")
    public DataResult getListBySort(Integer Sort) {
        //1升0降
        List<Category> list = service.getListBySort(Sort);
        return DataResult.success(list);
    }

    @ApiOperation("删除一条数据")
    @DeleteMapping("/deleteList/{id}")
    public DataResult deleteList(@PathVariable("id") @NotNull(message = "id不能为空")Integer categoryId){
        System.out.println(categoryId);
        int sucess=service.deleteList(categoryId);
        return sucess==1?DataResult.success() : DataResult.getResult(1, "删除失败");
    }
}

